package pong;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import pong.event.Cancelar;
import pong.event.Crear;
import pong.event.DoubleClick;
import pong.event.SaveWindow;
import pong.event.Unirse;
import pong.lan.Lan;
import pong.lan.ListItem;
import pong.rmi.ComRemota;
import pong.ui.PongGame;
import pong.util.Observador;
import pong.util.Util;

public class Main {
	public static JFrame ventana;
	
	public static JList<ListItem> list;
	public static JTextField username;
	public static JScrollPane scroll;
	public static JPanel buttons;
	public static JLabel lab;
	public static JPanel buttons2;
	public static PongGame pong;
	
	public static void main(String args[]) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException{
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		System.setSecurityManager(new RMISecurityManager());
		ventana = new JFrame("Pong");
		ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		ventana.addWindowListener(new SaveWindow());
		
		username = new JTextField(Util.read());
		username.setCaretPosition(username.getText().length());
		username.setBorder(BorderFactory.createTitledBorder("Username"));
		
		list = new JList<>();
		list.addMouseListener(new DoubleClick(list));
		scroll = new JScrollPane(list);
		
		buttons = new JPanel();
		JButton acceptar = new JButton("Crear");
		JButton unirse = new JButton("Unirse");
		buttons.add(acceptar);
		buttons.add(unirse);
		acceptar.addActionListener(new Crear());
		unirse.addActionListener(new Unirse());
		
		add();
		Dimension dim = ventana.getToolkit().getScreenSize();
		int w = dim.width;
		int h = dim.height;
		ventana.setBounds((w-920)/2, (h-620)/2, 920, 620);
		ventana.setResizable(false);
		ventana.setVisible(true);
		Lan.iniciar();
	}
	public static void add(){
		ventana.setLayout(new BorderLayout(20, 20));
		ventana.add(username, "North");
		ventana.add(scroll, "Center");
		ventana.add(buttons, "South");
	}

	public static void crear() {
		ventana.remove(username);
		ventana.remove(scroll);
		ventana.remove(buttons);
		ventana.getRootPane().revalidate();
		
		lab = new JLabel("Esperando retador...");
		lab.setVerticalAlignment(JLabel.CENTER);
		lab.setHorizontalAlignment(JLabel.CENTER);
		ventana.add(lab);
		JButton cancelar = new JButton("Cancelar");
		buttons2 = new JPanel();
		buttons2.add(cancelar);
		cancelar.addActionListener(new Cancelar());
		ventana.add(buttons2,"South");
		try {
			Lan.localhost.setGame(true);
			Lan.actualizarListaGeneral();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		ventana.repaint();
		
	}
	public static void cancelar() {
		try{
			Lan.localhost.setGame(false);
			Lan.actualizarListaGeneral();
		}catch (Exception e) {}
		ventana.remove(lab);
		ventana.remove(buttons2);
		ventana.getRootPane().revalidate();
		
		ventana.add(username, "North");
		ventana.add(scroll);
		ventana.add(buttons,"South");
		
		ventana.repaint();
	}
	public static void unirse(ListItem item) {
		try{
			if(item.computadora.irJuego(Lan.localhost)){
				irJuego(item.computadora, true);
				Lan.actualizarListaGeneral();
			}else{
				JOptionPane.showMessageDialog(null, "No se puede conectar");
			}
		}catch(Exception e){}
	}
	public static void irJuego(ComRemota host, boolean esRetador) {
		
		if(lab != null) ventana.remove(lab);
		if(buttons2 != null) ventana.remove(buttons2);
		if(username != null) ventana.remove(username);
		if(scroll != null) ventana.remove(scroll);
		if(buttons != null) ventana.remove(buttons);
		
		new Observador(host).start();
		pong = new PongGame(host, esRetador);
		ventana.add(pong);
		ventana.getRootPane().revalidate();
		ventana.repaint();
		pong.grabFocus();
	}
	public static void verLista() {
		ventana.remove(pong);
		
		ventana.getRootPane().revalidate();
		
		ventana.add(username, "North");
		ventana.add(scroll);
		ventana.add(buttons,"South");
		
		ventana.repaint();
	}
}







